package com.cs.manaer.controller;

import com.cs.manaer.controller.req.CsmgUserReqMsg;
import com.cs.manaer.controller.req.MGLoginReqMsg;
import com.cs.manaer.controller.req.ModifyPassReqMsg;
import com.cs.manaer.controller.res.MGLoginResMsg;
import com.cs.manaer.service.LoginService;
import com.lin.common.error.CustomRuntimeException;
import com.lin.common.error.ErrorCode;
import com.lin.common.rest.ResMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

@Api(tags = "登录注册入口")
@RestController
@RequestMapping("/unverify")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation("登录接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = MGLoginResMsg.class)
    })
    @PostMapping("/login")
    public ResMsg<MGLoginResMsg> login(@Valid @RequestBody MGLoginReqMsg user) throws Exception {
        ResMsg<MGLoginResMsg> urs = new ResMsg<>();
        MGLoginResMsg ur = loginService.login(user.getAccNo(), user.getPassword());
        return urs.withData(ur);
    }

    @ApiOperation("注册接口")
    @PostMapping("/register")
    public ResMsg<Integer> register(@Valid @RequestBody CsmgUserReqMsg reqMsg) throws InvalidKeySpecException, NoSuchAlgorithmException, CustomRuntimeException {
        if (StringUtils.isBlank(reqMsg.getMobileNo()) || StringUtils.isBlank(reqMsg.getEmail())) {
            throw new CustomRuntimeException(ErrorCode.EMAIL_OR_MOBILE_NOT_NULL, ErrorCode.EMAIL_OR_MOBILE_NOT_NULL.getMessage());
        }
//        if (!CommonUtils.isPhone(reqMsg.getMobile())) {
//            throw new CustomRuntimeException(ErrorCode.MOBILE_FORMAT_ERROR, ErrorCode.MOBILE_FORMAT_ERROR.getMessage());
//        }
        if (StringUtils.isNotBlank(reqMsg.getEmail()) && null != loginService.verifyEmail(reqMsg.getEmail())) {
            throw new CustomRuntimeException(ErrorCode.CUSTOMER_EXISTS, ErrorCode.CUSTOMER_EXISTS.getMessage());
        }
        if (loginService.verifyUser(reqMsg.getAccNo()) == 1) {
            throw new CustomRuntimeException(ErrorCode.CUSTOMER_EXISTS, ErrorCode.CUSTOMER_EXISTS.getMessage());
        }
        ResMsg<Integer> urs = new ResMsg<>();
        Integer i = loginService.register(reqMsg);
        return urs.withData(i);
    }

    @ApiOperation("忘记密码")
    @ApiImplicitParam(paramType = "path", name = "email", value = "邮件地址", required = true)
    @ApiResponses({
            @ApiResponse(code = 200, message = "data返回true则发送邮箱成功，否则发送邮箱失败")
    })
    @GetMapping("/forget/{email}")
    public ResMsg<Boolean> forgetPassword(@PathVariable String email) throws InvalidKeySpecException, NoSuchAlgorithmException, CustomRuntimeException {
        ResMsg<Boolean> urs = new ResMsg<>();
        return urs.withData(loginService.forgetPassword(email));
    }

    @PostMapping("/modifypass")
    public ResMsg<Integer> modifypass(@Valid @RequestBody ModifyPassReqMsg reqMsg) throws CustomRuntimeException {
        if (!StringUtils.equals(reqMsg.getConfirmpass(), reqMsg.getPass())) {
            throw new CustomRuntimeException(ErrorCode.PASSWORD_NOT_EQUALLY, ErrorCode.PASSWORD_NOT_EQUALLY.getMessage());
        }
        ResMsg<Integer> urs = new ResMsg<>();
        return urs.withData(loginService.modifypass(reqMsg.getEmail(), reqMsg.getPass(), reqMsg.getToken()));
    }


    @ApiOperation("查询账户是否存在")
    @ApiImplicitParam(paramType = "path", name = "email", value = "账号", required = true)
    @ApiResponses({
            @ApiResponse(code = 200, message = "data返回0则找不到账号，否则账户存在")
    })
    @GetMapping("/verifyNo/{accNo}")
    public ResMsg<Integer> verifyCustomer(@PathVariable String accNo) throws InvalidKeySpecException, NoSuchAlgorithmException, CustomRuntimeException {
        ResMsg<Integer> urs = new ResMsg<>();
        return urs.withData(loginService.verifyUser(accNo));
    }

}
